define(["jquery", "easy-admin", "echarts", "echarts-theme", "miniAdmin", "miniTab"], function ($, ea, echarts, undefined, miniAdmin, miniTab) {

    var init = {
        table_elem: '#currentTable',
        index_url: 'index',
    };

    var Controller = {
        index: function () {
            miniTab.listen();
            ea.listen();
            initChart();

            $('#search').click(() => {
                initChart();
            })
        },
    };

    function initChart() {

        var start_time = $('#start_time').val();
        var end_time = $('#end_time').val();
        $.ajax({
                type: "get",
                data: {start_time, end_time},
                url: init.index_url,
                success: function (data) {
                    $('#start_time').val(data.params.start_time);
                    $('#end_time').val(data.params.end_time);
                    $('#total_nums').text(data.total_nums);

                    let html = '';
                    let num = 1;
                    data.data.forEach(function(e){
                        num++;
                        html += `<tr>
                            <td>${e.name}</td>
                            <td>${e.value}</td>
                        </tr>`;
                    });
                    $('#tbody').html(html);

                    echarts.registerMap('China', data.chars);
                    let echartsRecords = echarts.init(document.getElementById('echarts-records'));
                    let optionRecords = {

                        title: {
                            // text: '全省观看用户分布图',
                        },
                        tooltip: {
                            trigger: 'item',
                            formatter: '观看用户数 {c} '
                            // formatter: '{b}<br/>{c} 观看用户数'
                        },
                        toolbox: {
                            show: false,
                            orient: 'vertical',
                            left: 'right',
                            top: 'center',
                            feature: {
                                dataView: {readOnly: false},
                                restore: {},
                                saveAsImage: {}
                            }
                        },
                        visualMap: {
                            min: 0,
                            max: data.max_num,
                            text: ['高', '低'],
                            left: 'right',
                            top: 'center',
                            inRange: {
                                color: ['lightskyblue', 'yellow', 'orangered']
                            }
                        },
                        series: [
                            {
                                // name: '全省观看用户分布图',
                                type: 'map',
                                mapType: 'China', // 自定义扩展图表类型
                                label: {
                                    show: true
                                },
                                data: data.data
                            }
                        ]
                    };
                    echartsRecords.setOption(optionRecords);

                    window.addEventListener("resize", function () {
                        echartsRecords.resize();
                    });
                }
            }
        )
    }

    return Controller;
});
